package report.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import report.dao.repository.DashboardsRepository;
import report.dao.util.ReturnMsgUtil;
import report.dao.vo.dashboard.DashboardVO;
import report.service.dashboard.IDashboardService;

@Controller
public class DashboardController {

    @Autowired
    private DashboardsRepository dashboardsRepository;
    @Autowired
    private IDashboardService dashboardService;

    // 获取dashboard
    @RequestMapping(value = "/dashboard/all")
    @ResponseBody
    public ReturnMsgUtil queryDashboardAll() {
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( dashboardsRepository.findAll() );
        res.setStatus("success");
        return res;
    }

    // 根据目录id获取dashboard
    @RequestMapping(value = "/dashboard/{catalogId}")
    @ResponseBody
    public ReturnMsgUtil queryDashboardByCataId(@PathVariable String catalogId) {
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( dashboardsRepository.findAllByPid(catalogId) );
        res.setStatus("success");
        return res;
    }

    // 添加
    @PostMapping(value = "/dashboard/create/{pid}")
    @ResponseBody
    public ReturnMsgUtil createDashboard(@PathVariable String pid, @RequestBody DashboardVO form){
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( dashboardService.createDashboard(pid,form) );
        res.setStatus("success");
        return res;
    }


    // 更新dashboard
    @PutMapping(value = "/dashboard/update/{id}")
    @ResponseBody
    public ReturnMsgUtil updateDashboard(@PathVariable String id, @RequestBody DashboardVO form){
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( dashboardService.updateDashboard(id, form));
        res.setStatus("success");
        return res;
    }

    // 删除dashboard
    @DeleteMapping(value = "/dashboard/delete/{id}")
    @ResponseBody
    public ReturnMsgUtil deleteDashboard(@PathVariable String id){
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( dashboardService.deleteDashboard(id));
        res.setStatus("success");
        return res;
    }
}
